﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Registrator;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using DevExpress.XtraEditors.Drawing;
using DevExpress.Utils;

namespace Sunboxsoft.ASMS.Client.UI.Common.RepositoryEdit
{
    [UserRepositoryItem("Register")]
    public class MyRepositoryEdit : RepositoryItemTextEdit
    {
        static MyRepositoryEdit()
        {
            Register();
        }

        public MyRepositoryEdit() 
        {
            this.ReadOnly = true;
           
        }

        internal const string EditorName = "MyRepositoryEdit";

        public static void Register()
        {
            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(EditorName, typeof(MyEdit),
                typeof(MyRepositoryEdit), typeof(DevExpress.XtraEditors.ViewInfo.TextEditViewInfo),
                new MyStatusLabelPainter(), true, null,
                typeof(DevExpress.Accessibility.TextEditAccessible)));
        }

        public override string EditorTypeName
        {
            get { return EditorName; }
        }

        public override BaseEdit CreateEditor()
        {
            return base.CreateEditor();
        }

    }

    public class MyEdit : TextEdit
    {
        static MyEdit()
        {
            MyRepositoryEdit.Register();
        }
        public MyEdit()
        {
            this.Enabled = false;
        }

        protected override void OnBindingContextChanged(System.EventArgs e)
        {
            //this.DataBindings[0].DataSource
            base.OnBindingContextChanged(e);
        }

        public string DisplayText { set; get; }

        public override string EditorTypeName
        {
            get { return MyRepositoryEdit.EditorName; }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new MyRepositoryEdit Properties
        {
            get { return base.Properties as MyRepositoryEdit; }
        }

        protected override void OnAfterUpdateViewInfo()
        {
            base.OnAfterUpdateViewInfo();
        }
        //protected override void OnClickButton(DevExpress.XtraEditors.Drawing.EditorButtonObjectInfoArgs buttonInfo)  
        //{  
        //    ShowPopupForm();  
        //    base.OnClickButton(buttonInfo);  
        //}  
        //protected virtual void ShowPopupForm()  
        //{  
        //    using (Form form = new Form())  
        //    {  
        //        form.StartPosition = FormStartPosition.Manual;  
        //        form.Location = this.PointToScreen(new Point(0, Height));  
        //        form.ShowDialog();  
        //    }  
        //}  
    }

    public class MyStatusLabelPainter : TextEditPainter
    {
        public MyStatusLabelPainter()
        {
        }

        protected override void DrawContent(ControlGraphicsInfoArgs info)
        {

            info.Graphics.FillRectangle(Brushes.Green, new Rectangle(0, 0, 30, 25));
            info.Graphics.FillRectangle(Brushes.Red, new Rectangle(35, 0, 30, 25));
            info.Graphics.FillRectangle(Brushes.Gray, new Rectangle(70, 0, 30, 25));
        }

        protected override void DrawMatchedString(ControlGraphicsInfoArgs info, Rectangle bounds, string text, string matchedText, AppearanceObject appearance, bool invert, int containsIndex)
        {
        }
        protected override void DrawString(ControlGraphicsInfoArgs info, Rectangle bounds, string text, AppearanceObject appearance)
        {
        }
        protected override void DrawText(ControlGraphicsInfoArgs info)
        {
        }
        protected override void DrawTextBoxArea(ControlGraphicsInfoArgs info)
        {
        }
    }
}